<?php
/*
  处理工作汇总相关操作s
*/
namespace Admin\Controller\Work;
use Think\Page;

use Admin\Model\Doctor\DoctorAppointModel;
use Admin\Controller\AdminController;
use Home\Common\SalemanagerHelper;
use Home\Model\SaleManager\County\SalemanagerModel;
use Home\Model\SaleManager\Clinic\ClinicModel;

class IndexController extends AdminController
{
    
	 public function index()
	 {
         $resturn_array = array();
         $count = M("saleman_apply")->where("1=1")->count();
         $page = new Page($count,20);
         $show = $page->show();
         $resturn = M("saleman_apply")->join("ymt_saleman on ymt_saleman.id=ymt_saleman_apply.saleman_id")->field("ymt_saleman.real_name,ymt_saleman_apply.apply_time,ymt_saleman_apply.reply_status,ymt_saleman_apply.superior_id,ymt_saleman_apply.apply_content,ymt_saleman_apply.id,ymt_saleman_apply.type")->limit($page->firstRow.','.$page->listRows)->select();
        
         $resturn_array= array_reduce($resturn,function($result,$data){
            $result[] = $data['superior_id'];
            return $result;
         });
         $arr_id = array_unique($resturn_array);
         
         
         $superior_name = M("saleman")->query("select real_name from ymt_saleman where id in (".implode(",",$arr_id).")");
         $arr = array_combine($arr_id,$superior_name);
         
         foreach($resturn as $key=>$value)
         {
         	  $resturn[$key]['superior_name'] = $arr[$value['superior_id']]['real_name'];
         }
         $this->assign("count",$count);
         $this->assign("page",$show);
         $this->assign("lists",$resturn);
	 	 $this->display();
	 }
	 public function delete()
	 {
	 	 if(!isset($_GET['id']) || empty($_GET['id']))
	 	 {
	 	 	  $this->ajaxReturn(array('code'=>0,'message'=>'参数错误'));
	 	 	  die();
	 	 }
	 	 $flag = M("saleman_apply")->where("id=".$_GET['id'])->delete();
	 	 if($flag)
	 	 {
	 	 	  $this->ajaxReturn(array('code'=>1,'message'=>'删除成功'));
	 	 	  return;
	 	 }
	 	  $this->ajaxReturn(array('code'=>1,'message'=>'删除失败'));
	 }
	 public function edit()
	 {
	 	  if(IS_AJAX)
	 	  {
	 	  	  $id = $_GET['id'];
	 	  	  if(isset($_POST['reply_content']) && !empty($_POST['reply_content']))
	 	  	  {
	 	  	  	  $_POST['reply_time'] = time();
	 	  	  }
	 	  	  $flag = M("saleman_apply")->where("id=".$id)->save($_POST);
	 	  	  if($flag)
	 	  	  {
	 	  	  	   $this->ajaxReturn(array('code'=>'1','message'=>'更新成功'));
	 	  	  	   exit;
	 	  	  }
	 	  	  $this->ajaxReturn(array('code'=>0,'message'=>'更新失败'));
	 	  }else{
	 	  $id = $_GET['id'];
	 	  $result = M("saleman_apply")->where("id=".$id)->select();

	 	  $this->assign("result",$result);
	 	  $this->display();
	 	}
	 }
	 public function add()
	 {
	 	   if(!IS_AJAX)
	 	   {
	 	   	   $saleman_res = M("saleman")->where("1=1")->field("real_name,id")->select();
	 	   	   $this->assign("saleman",$saleman_res);
	 	   	   $this->display();
	 	   }else{
	 	   	   if(isset($_POST['apply_content']) && !empty($_POST['apply_content']))
	 	   	   {
	 	   	   	   $_POST['copy_id'] = 403;
	 	   	   	   $_POST['apply_time'] = time();
	 	   	   	   $_POST['superior_id'] = M("saleman")->where("id=".$_POST['saleman_id'])->getField("superior_id");
                   
                   $flag = M("saleman_apply")->data($_POST)->add();
                   if($flag)
                   {
                   	   $this->ajaxReturn(array('code'=>1,'message'=>'操作成功'));
                   }else{
                   	   $this->ajaxReturn(array('code'=>0,'message'=>'操作失败'));
                   }
                   return;
	 	   	   }
	 	   	   $this->ajaxReturn(array('code'=>0,'message'=>'请完善内容'));
	 	   	   return;
	 	   }
	 }
}
?>